package org.nowcoder.huawei.other;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * HJ20: 密码验证合格程序
 */
public class HJ20 {

    //没有长度大于2的包含公共元素的子串重复
    public static boolean repeatSubString(String line) {
        for (int i = 3; i <= line.length() / 2; i++) {
            for (int j = 0; j < line.length() - i; j++) {
                String tmp = line.substring(j, j + i);
                System.out.println(tmp);
                if (line.substring(i + j).contains(tmp)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean passwordType(boolean[] flags) {
        int count = 0;
        for (boolean flag : flags) {
            if (flag) {
                count++;
            }
        }
        return count >= 3;
    }

    public static void hj20() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;

        //重复读入多行
        while ((line = br.readLine()) != null) {
            //1. 长度判断
            if (line.length() < 8) {
                System.out.println("NG");
            } else {
                //密码种类判断
                //0: 大写字母 1: 小写字母 2：数字 3：其他字符
                boolean[] flags = new boolean[4];
                for (char c : line.toCharArray()) {
                    if ('A' <= c && c <= 'Z') {
                        flags[0] = true;
                    } else if ('a' <= c && c <= 'z') {
                        flags[1] = true;
                    } else if ('0' <= c && c <= '9') {
                        flags[2] = true;
                    } else {
                        flags[3] = true;
                    }
                }
                //至少含有三种
                if (passwordType(flags)) {
                    //不能有长度大于2的包含公共元素的子串重复
                    if (repeatSubString(line)) {
                        System.out.println("OK");
                    } else {
                        System.out.println("NG");
                    }
                } else {
                    System.out.println("NG");
                }
            }
        }
    }
}
